﻿using TjZheng.News.Business;
using TjZheng.News.DataEntity;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Cache;
using TjZheng.Framework;
using TjZheng.Base;
using System.Text.RegularExpressions;

namespace TjZheng.News.Process
{
    public partial class ArticleInfoProcess
    {

        public ArticlePlatformType ArticlePlatformType { get; private set; }

        public ArticleInfoProcess(ArticlePlatformType ArticlePlatformType = ArticlePlatformType.Phone)
        {
            this.ArticlePlatformType = ArticlePlatformType;
        }

        public virtual bool ExistTableArticleRelatedAll(int parentId, int categoryId)
        {
            BizBase objBizBase = new BizBase();
            var objDataTable = objBizBase.ExecuteDataTableCache("", TimeSpan.FromMinutes(1), "TjZheng_News", "ApiGroup_News", "TjZheng_ExistTable_ArticleRelatedAll", null, new { CategoryId = categoryId, ParentId = parentId });
            return objDataTable.IsNoNull() && objDataTable.Rows.Count == 3;
        }

        public virtual bool ExistTableArticleRelatedOnly(int categoryId)
        {
            BizBase objBizBase = new BizBase();
            var objDataTable = objBizBase.ExecuteDataTableCache("", TimeSpan.FromMinutes(1), "TjZheng_News", "ApiGroup_News", "TjZheng_ExistTable_ArticleRelatedOnly", null, new { CategoryId = categoryId });
            return objDataTable.IsNoNull() && objDataTable.Rows.Count == 2;
        }

        public virtual bool ExistTableArticleCollect(int parentId)
        {
            BizBase objBizBase = new BizBase();
            var objResult = objBizBase.ExecuteScalarCache("", TimeSpan.FromMinutes(1), "TjZheng_News", "ApiGroup_News", "TjZheng_ExistTable_ArticleCollect", null, new { ParentId = parentId });
            return objResult != null;
        }

        public virtual bool ExistTableArticleInfo(int categoryId)
        {
            BizBase objBizBase = new BizBase();
            var objResult = objBizBase.ExecuteScalarCache("", TimeSpan.FromMinutes(1), "TjZheng_News", "ApiGroup_News", "TjZheng_ExistTable_ArticleInfo", null, new { CategoryId = categoryId });
            return objResult != null;
        }

        public virtual bool ExistTableArticleInfoData(int categoryId)
        {
            BizBase objBizBase = new BizBase();
            var objResult = objBizBase.ExecuteScalarCache("", TimeSpan.FromMinutes(1), "TjZheng_News", "ApiGroup_News", "TjZheng_ExistTable_ArticleInfoData", null, new { CategoryId = categoryId });
            return objResult != null;
        }

        public virtual bool ExistTableCommentInfo(int categoryId)
        {
            BizBase objBizBase = new BizBase();
            object obj = objBizBase.ExecuteApiCache("", TimeSpan.FromMinutes(1), "TjZheng_News", "ApiGroup_News", "TjZheng_ExistTable_CommentInfo", null, new { CategoryId = categoryId });
            return obj != null;
        }

        public virtual ArticleInfo GetArticleInfo(int categoryId, int articleId)
        {
            BizArticleInfo objBizArticleInfo = new BizArticleInfo(categoryId.ToString());
            return objBizArticleInfo.GetRecordCache("", articleId, TimeSpan.FromMinutes(1));
        }

        public virtual ArticleResultInfo GetArticleResultInfo(int categoryId, int articleId, bool isLoadArticleContent = true, bool isLoadPictureCollection = false, bool isConvertTargetUrl = true)
        {
            ArticleResultInfo objArticleResultInfo = null;
            ArticleInfo objArticleInfo = this.GetArticleInfo(categoryId, articleId);
            if (objArticleInfo == null)
            {
                return objArticleResultInfo;
            }
            objArticleResultInfo = objArticleInfo.CreateArticleResultInfo(isLoadArticleContent, isLoadPictureCollection);
            if (objArticleResultInfo == null)
            {
                return objArticleResultInfo;
            }
            if (isConvertTargetUrl)
            {
                objArticleResultInfo.TargetUrl = objArticleInfo.ConvertTargetUrl(null, this.ArticlePlatformType);
            }
            return objArticleResultInfo;
        }

        public virtual ArticleCollect GetArticleCollect(int parentId, string collectId)
        {
            BizArticleCollect objBizArticleCollect = new BizArticleCollect(parentId.ToString());
            return objBizArticleCollect.GetRecordCache("", collectId, TimeSpan.FromMinutes(1));
        }

        public virtual ArticleResultInfo GetArticleResultInfo(int parentId, string collectId, bool isLoadArticleContent = true, bool isLoadPictureCollection = false, bool isConvertTargetUrl = true)
        {
            ArticleResultInfo objArticleResultInfo = null;
            ArticleCollect objArticleCollect = this.GetArticleCollect(parentId, collectId);
            if (objArticleCollect == null)
            {
                return objArticleResultInfo;
            }
            objArticleResultInfo = objArticleCollect.CreateArticleResultInfo(isLoadArticleContent, isLoadPictureCollection);
            if (objArticleResultInfo == null)
            {
                return objArticleResultInfo;
            }
            if (isConvertTargetUrl)
            {
                objArticleResultInfo.TargetUrl = objArticleCollect.ConvertTargetUrl(null, this.ArticlePlatformType);
            }
            return objArticleResultInfo;
        }

        public virtual void IncreaseViewCount(int parentId, int categoryId, int articleId, int incryBy = 1)
        {
            BizBase objBizBase = new BizBase();
            objBizBase.ExecuteNonQuery("TjZheng_News", "ApiGroup_News", "TjZheng_IncreaseCount_Article", new { ArticleId = articleId, CollectId = string.Format("{0}-{1}", categoryId, articleId) }, new { ParentId = parentId, CategoryId = categoryId, RelatedCount = "ViewCount", Increase = incryBy });
        }

        public virtual void IncreaseDiggCount(int parentId, int categoryId, int articleId, int incryBy = 1)
        {
            BizBase objBizBase = new BizBase();
            objBizBase.ExecuteNonQuery("TjZheng_News", "ApiGroup_News", "TjZheng_IncreaseCount_Article", new { ArticleId = articleId, CollectId = string.Format("{0}-{1}", categoryId, articleId) }, new { ParentId = parentId, CategoryId = categoryId, RelatedCount = "DiggCount", Increase = incryBy });
        }

        public virtual void IncreaseBuryCount(int parentId, int categoryId, int articleId, int incryBy = 1)
        {
            BizBase objBizBase = new BizBase();
            objBizBase.ExecuteNonQuery("TjZheng_News", "ApiGroup_News", "TjZheng_IncreaseCount_Article", new { ArticleId = articleId, CollectId = string.Format("{0}-{1}", categoryId, articleId) }, new { ParentId = parentId, CategoryId = categoryId, RelatedCount = "BuryCount", Increase = incryBy });
        }
    }
}
